import api from '@/api'
import { deepCopy, setPage } from '@/utils/util'
import fetch from '@/utils/network/fetch'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 0,
    filter: {
        enabled: null,
        classIds: null,
        name: null
    }
}

const state = {
    data: {
        businessClasses: [],
        businesses: [],
        lastBusinesses: [],
        lastDocs: []
    },
    ui: {
        page: deepCopy(initPage)
    }
}

// getters
const getters = {
    businessClasses: state => state.data.businessClasses,
    businesses: state => state.data.businesses,
    lastBusinesses: state => state.data.lastBusinesses,
    lastDocs: state => state.data.lastDocs,
    businessTotal: state => state.ui.page.total
}

// actions
const actions = {
    getBusinessClasses({ commit }) {
        api.getClassifications('key_class_business', {
            loading: true,
            success: businessClasses => {
                commit('setBusinessClasses', { businessClasses })
            }
        })
    },
    saveBusinessClass({ dispatch }, data) {
        api.saveClassification(
            { ...data, classType: 'key_class_business' },
            {
                success: () => {
                    dispatch('getBusinessClasses')
                }
            }
        )
    },
    removeBusinessClass({ dispatch }, id) {
        api.removeClassification(id, {
            success: () => {
                dispatch('getBusinessClasses')
            }
        })
    },
    getBusinesses({ commit, state }, page) {
        api.getBusinesses(page ? page : state.ui.page, {
            loading: true,
            success: res => {
                const { pages, total } = res
                commit('setBusinessPage', { pages, total })
                commit('setBusinesses', res)
            }
        })
    },
    saveBusiness({ dispatch }, data) {
        api.saveBusiness(data, {
            success: () => {
                dispatch('getBusinesses')
            }
        })
    },
    removeBusinesses({ dispatch }, ids) {
        api.removeBusinesses(ids, {
            success: () => {
                dispatch('getBusinesses')
            }
        })
    },
    getLastBusinesses({ commit }) {
        fetch('/api/homepage/info/last_businesses', {
            success: lastBusinesses => {
                commit('setLastBusinesses', { lastBusinesses })
            }
        })
    },
    getLastDocs({ commit }) {
        fetch('/api/homepage/info/docs', {
            success: lastDocs => {
                commit('setLastDocs', { lastDocs })
            }
        })
    }
}

// mutations
const mutations = {
    setBusiness: (state, payload) => {
        state.business = payload.business
    },
    setBusinessClasses: (state, payload) => {
        state.data.businessClasses = payload.businessClasses
    },
    setBusinessPage: (state, page) => {
        page && setPage(state.ui.page, page)
    },
    setBusinesses: (state, payload) => {
        state.data.businesses = payload.content
    },
    setLastBusinesses: (state, payload) => {
        state.data.lastBusinesses = payload.lastBusinesses
    },
    setLastDocs: (state, payload) => {
        state.data.lastDocs = payload.lastDocs
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
